//
//  YZLSelectTableHeader.m
//  Car
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLSelectTableHeader.h"
#import "DHeaderCollectionCell.h"
#import "TieDeatailViewController.h"
#import "DiscussViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface YZLSelectTableHeader ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collection;

@property (nonatomic, retain)UIPageControl *page;

@property (nonatomic, retain)NSTimer *timer;


@end

@implementation YZLSelectTableHeader

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.itemSize = CGSizeMake(WIDTH, 200*HEIGHT/667);
        flow.minimumLineSpacing = 0;
        flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200*HEIGHT/667) collectionViewLayout:flow];
        [self addSubview:self.collection];
        self.collection.delegate = self;
        self.collection.dataSource = self;
        [self.collection registerClass:[DHeaderCollectionCell class] forCellWithReuseIdentifier:@"select"];
        self.collection.backgroundColor = [UIColor whiteColor];
        self.collection.pagingEnabled = YES;
        self.collection.contentOffset = CGPointMake(WIDTH, 0);
        self.collection.showsHorizontalScrollIndicator = NO;
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(scroll:) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
        self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(10*WIDTH/375
                                                                   
                                                                   , 180*HEIGHT/667, 80*WIDTH/375
                                                                   
                                                                   , 20*HEIGHT/667)];
        [self.page addTarget:self action:@selector(page:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:self.page];
        self.page.pageIndicatorTintColor = [UIColor blackColor];
        self.page.currentPageIndicatorTintColor = [UIColor whiteColor];

    }
    return self;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    YZLDiscuss *discuss = [self.arr objectAtIndex:indexPath.row];
    TieDeatailViewController *tie = [[TieDeatailViewController alloc] init];
    discuss.postId = discuss.focusId;
    tie.discuss = discuss;
    DiscussViewController *disViewController = [DiscussViewController shareDiscussViewController];
    [disViewController.navigationController pushViewController:tie animated:YES];
}

- (void)scroll:(NSTimer *)time {
    if (self.arr.count >0) {
    if (self.collection.contentOffset.x == WIDTH*(self.arr.count-1)) {


    self.collection.contentOffset = CGPointMake(WIDTH, 0);
        [UIView animateWithDuration:.35 animations:^{
            [self.collection setContentOffset:CGPointMake(self.collection.contentOffset.x +WIDTH, 0) animated:YES];
        }];

    }
    else if (self.collection.contentOffset.x > WIDTH*(self.arr.count-1)) {
        //矫正
        self.collection.contentOffset = CGPointMake(WIDTH, 0);
        [UIView animateWithDuration:.35 animations:^{
            [self.collection setContentOffset:CGPointMake(self.collection.contentOffset.x +WIDTH, 0) animated:YES];
        }];
    } else {
        [UIView animateWithDuration:.35 animations:^{
            [self.collection setContentOffset:CGPointMake(self.collection.contentOffset.x +WIDTH, 0) animated:YES];
        }];

    }
        if (self.collection.contentOffset.x/WIDTH>self.arr.count-3) {
            self.page.currentPage = 0;
        } else {
            self.page.currentPage = self.collection.contentOffset.x/WIDTH;
        }
    }
}



- (void)setArr:(NSArray *)arr {
    if (_arr != arr) {
        _arr = arr;
    }
    [self.collection reloadData];
    self.page.numberOfPages = self.arr.count-2;
    self.collection.contentOffset = CGPointMake(WIDTH, 0);
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (self.collection.contentOffset.x == 0) {
        [self.collection setContentOffset:CGPointMake(WIDTH*(self.arr.count-2), 0) animated:NO];
        self.page.currentPage = self.arr.count;
    } else if (self.collection.contentOffset.x == WIDTH*(self.arr.count-1)) {
        [self.collection setContentOffset:CGPointMake(WIDTH, 0) animated:NO];
        self.page.currentPage = 0;
    }
    else {
        self.page.currentPage = self.collection.contentOffset.x/WIDTH-1;
    }

}

- (void)page:(UIPageControl *)page {
    [self.collection setContentOffset:CGPointMake(WIDTH*page.currentPage+WIDTH, 0) animated:YES];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DHeaderCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"select" forIndexPath:indexPath];
    YZLDiscuss *discuss = [self.arr objectAtIndex:indexPath.item];
    cell.discuss = discuss;
    return cell;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
